/*
 * ESBoolean.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Evaluator;

/**
 * Boolean primitive value.
 */
public final class ESBoolean extends ESPrimitive {

	// There is only one true and one false value (allowing
	// efficient comparisons)
	/** The true value. */
	private static ESBoolean trueValue = new ESBoolean();

	/** The false value. */
	private static ESBoolean falseValue = new ESBoolean();

	/**
	 * Instantiates a new eS boolean.
	 */
	private ESBoolean() {
	}

	/**
	 * Create a boolean primitive (either true or false) by returning the
	 * predefined (unique) true or false values.
	 * 
	 * @param value
	 *            the value
	 * 
	 * @return either trueValue or falseValue
	 */
	static public ESBoolean makeBoolean(boolean value) {
		return value ? trueValue : falseValue;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toDetailString()
	 */
	public String toDetailString() {
		return "ES:<" + (this == trueValue ? "true" : "false") + ">";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeOf()
	 */
	public int getTypeOf() {
		return EStypeBoolean;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeofString()
	 */
	public String getTypeofString() {
		return "boolean";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return this == trueValue ? "true" : "false";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#doubleValue()
	 */
	public double doubleValue() {
		return this == trueValue ? 1 : 0;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#booleanValue()
	 */
	public boolean booleanValue() {
		return this == trueValue;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESBoolean()
	 */
	public ESValue toESBoolean() {
		return this;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESObject(org.scriptme.ecmascript.interpreter.Evaluator)
	 */
	public ESValue toESObject(Evaluator evaluator) throws EcmaScriptException {
		BooleanPrototype theObject = null;
		ESObject bp = evaluator.getBooleanPrototype();
		theObject = new BooleanPrototype(bp, evaluator);
		theObject.value = this;
		return theObject;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toJavaObject()
	 */
	public Object toJavaObject() {
		return new Boolean(this == trueValue);
	}

	// overrides
	/**
	 * returns true as we implement booleanValue without an evaluator.
	 * 
	 * @return true
	 */
	public boolean isBooleanValue() {
		return true;
	}
}
